package com.swift.maintenance.utils;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;

/**
 * Created by Administrator on 2018/4/9.
 */

public class PermissionUtil {

//    身体传感器
//            日历
//    摄像头
//            通讯录
//    地理位置
//            麦克风
//    电话
//            短信
//    存储空间

    public static final int CARAME = 1;
    public static final int WRITE = 2;
    public static final int LOCATION = 3;
    public static final String  NEVER_TIP = "你拒绝过此权限，请手动开启";

    /**
    * @desc 请求对应的权限
    * @author hym
    * @createDate 2018/4/9 17:04
    */
    public static void requestPermission(Activity activity,int perMissionType)
    {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return;

        switch (perMissionType)
        {
            case CARAME:
                //没有授权,判断权限申请是否曾经被拒绝过
                ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        perMissionType);
                break;
            case WRITE:
                ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        perMissionType);

                break;
            case LOCATION:
                ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                        perMissionType);

        }
    }

    public static void requestPermission(Fragment fragment,int perMissionType)
    {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return;

        switch (perMissionType)
        {
            case CARAME:
                //没有授权,判断权限申请是否曾经被拒绝过
                fragment.requestPermissions(new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        perMissionType);
                break;
            case WRITE:
                fragment.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, perMissionType);

                break;
            case LOCATION:
                fragment.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, perMissionType);

        }
    }

    /**
    * @desc 检查是否有对应的权限，如果没有则去请求
    * @author hym
    * @createDate 2018/4/9 16:58
    */
    public static boolean checkPermission(Object object,int perMissionType)
    {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)  return  true;

        if (object instanceof Activity)
        {
            Activity activity = (Activity) object;
            switch (perMissionType)
            {
                case CARAME:
                    if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA)
                            != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                            != PackageManager.PERMISSION_GRANTED)
                    {
                        requestPermission(activity,perMissionType);
                        return false;
                    }
                    else {
                        return true;
                    }
                case WRITE:
                    if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                            != PackageManager.PERMISSION_GRANTED )
                    {
                        requestPermission(activity,perMissionType);
                        return false;
                    }
                    else {
                        return true;
                    }
                case LOCATION:
                    if (ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION)
                            != PackageManager.PERMISSION_GRANTED )
                    {
                        requestPermission(activity,perMissionType);
                        return false;
                    }
                    else {
                        return true;
                    }
            }
        }
        else  if (object instanceof Fragment)
        {
            Fragment fragment = (Fragment) object;
            switch (perMissionType)
            {
                case CARAME:
                    if (ContextCompat.checkSelfPermission(fragment.getActivity(), Manifest.permission.CAMERA)
                            != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(fragment.getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)
                            != PackageManager.PERMISSION_GRANTED)
                    {
                        requestPermission(fragment,perMissionType);
                        return false;
                    }
                    else {
                        return true;
                    }
                case WRITE:
                    if (ContextCompat.checkSelfPermission(fragment.getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)
                            != PackageManager.PERMISSION_GRANTED )
                    {
                        requestPermission(fragment,perMissionType);
                        return false;
                    }
                    else {
                        return true;
                    }
                case LOCATION:
                    if (ContextCompat.checkSelfPermission(fragment.getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
                            != PackageManager.PERMISSION_GRANTED )
                    {
                        requestPermission(fragment,perMissionType);
                        return false;
                    }
                    else {
                        return true;
                    }
            }
        }



        ToastUtil.getInstances().showMsg(NEVER_TIP);
        return false;
    }
}
